package com.example.airquality.air.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.airquality.air.entity.District;
import com.example.airquality.air.entity.QualityIndex;
import com.example.airquality.air.service.IDistrictService;
import com.example.airquality.air.service.IQualityIndexService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hzl
 * @since 2021-05-18
 */
@Controller
@RequestMapping("/qualityIndex")
public class QualityIndexController {
    @Autowired
    private IQualityIndexService iQualityIndexService;
    @Autowired
    private IDistrictService iDistrictService;



    @RequestMapping("/toFindAll")
    public String toFindAll(@RequestParam(value = "current", defaultValue = "1") Long current,
                            @RequestParam(value = "size", defaultValue = "5") Long size,
                            Integer districtId,Model model,Integer qualityId){
        Page page = new Page(current,size);

            //OrderItem类中自带一下方法，其中包含排序，descs就是降序
            List<OrderItem> orders = OrderItem.descs("monitor_time");

        page.setOrders(orders);
        QualityIndex qualityIndexArg = new QualityIndex();
        qualityIndexArg.setDistrictId(districtId);

        List<QualityIndex> qualityByPage = iQualityIndexService.findAll(page,qualityIndexArg);
        page.setRecords(qualityByPage);
        model.addAttribute("qualityList",qualityByPage);
        model.addAttribute("pageInfo",page);
        //districtId是为了选项框里带值反选
        model.addAttribute("seldistrictId",districtId);
        model.addAttribute("qualityId",qualityId);
//      下面两行是实现主页下拉框
        List<District> districtList = iDistrictService.list();
        model.addAttribute("districtList",districtList);
        return "index";
    }

}

